package com.zhousc.springcloud;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class RequestCacheService {

    @Autowired
    private MyService service;

    /**
     * CacheResult 开启缓存结果功能
     * HystrixCommand-commandKey:设置缓存key方法签名
     * CacheKey:设置方法的缓存变量
     * @param name
     * @return
     */
    @CacheResult
    @HystrixCommand(commandKey = "cachekey")
    public Friend requestCache(@CacheKey String name){
        log.info("request cache "+ name);
        Friend friend = new Friend();
        friend.setName(name);
        friend = service.sayHiPost(friend);
        log.info("after requesting cache " + name);
        return friend;
    }
}
